package priv.pront.code.national.acwing.search.bfs;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:31
 */
public class Acwing1097_池塘计数 {

    static Queue<int[]> queue = new LinkedList<>();
    static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1}, dy = {0, 1, 1, 1, 0, -1, -1, -1};
    static int M, N, ans;
    static char[][] g;
    static boolean[][] st;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();
        M = scanner.nextInt();
        g = new char[N + 5][M + 5];
        st = new boolean[N + 5][M + 5];
        for(int i = 0; i < N; i++){
            g[i] = scanner.next().toCharArray();
        }
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M ;j++){
                if(!st[i][j] && g[i][j] == 'W'){
                    bfs(i, j);
                }
            }
        }
        System.out.println(ans);
    }

    private static void bfs(int row, int col){
        queue.add(new int[]{row, col});
        while(!queue.isEmpty()){
            int[] cur = queue.poll();
            for(int i = 0; i < 8; i++){
                int nx = cur[0] + dx[i];
                int ny = cur[1] + dy[i];
                if(nx >= 0 && nx < N && ny >= 0 && ny < M && g[nx][ny] == 'W' && !st[nx][ny]){
                    queue.add(new int[]{nx, ny});
                    st[nx][ny] = true;
                }
            }
        }
        ans++;
    }
}
